<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="workflowList" @selection-change="handleSelectionChange">
      <el-table-column label="申请人" align="center" prop="flowApply.applyName" />
      <el-table-column label="申请时间" align="center" prop="flowApply.applyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.flowApply.applyTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请类型" align="center" prop="flowApply.basicinfo.flowName" />
      <el-table-column label="备注" align="center" prop="flowApply.applyRemark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['approval:workflow:edit']"
          >审核</el-button>
        </template>
      </el-table-column>
    </el-table>


    <!-- 添加或修改工作流程对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body top="150vh">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="申请人" prop="applyName" disabled>
              <el-input placeholder="请输入内容" v-model="form.applyName" disabled></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="申请类型" prop="flowId">
              <el-select v-model="form.flowId" placeholder="请选择" disabled>
                <el-option
                  v-for="item in basicinfoList"
                  :key="item.id"
                  :label="item.flowName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="applyRemark" >
          <el-input
            type="textarea"
            disabled
            :rows="2"
            placeholder="请输入内容"
            v-model="form.applyRemark">
          </el-input>
        </el-form-item>
        <el-form-item label="类型" prop="flowCaiGouApply">
          <el-checkbox-group v-model="flowCaiGouApply.arrCaigou">
            <el-checkbox label="1">镀锌基建</el-checkbox>
            <el-checkbox label="2">镀锌生产线</el-checkbox>
            <el-checkbox label="3">办公用品</el-checkbox>
            <el-checkbox label="0">其他</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      <el-tabs  style="height: 300px">
        <el-tab-pane label="审核意见" >
          <el-input
            type="textarea"
            :rows="4"
            placeholder="请输入内容"
            v-model="approverRemark"
          >
          </el-input>
        </el-tab-pane>
        <el-tab-pane label="审核流程">
          <el-steps :active="nodeId" align-center finish-status="success" :process-status="processStyle"  >
            <el-step title="开始"></el-step>
            <el-step v-for ="item in flowList" :key="item.id" :title="item.nodeType==='1'?'审批人':'抄送人'"
                     description="description">
              <template slot="description" >
                <span>
                  <div class="course-style">{{item.approverUid}}</div>
                  <div class="course-style">{{parseTime(item.approvalTime, '{y}-{m}-{d} {h}:{i}')}}</div>
                  <div class="course-style" v-if="item.approverRemark!=null">意见：{{item.approverRemark}}</div>
                </span>
              </template>
            </el-step>
          </el-steps>
        </el-tab-pane>
      </el-tabs>
      </el-form>
      <el-dialog width="30%" title="转 交" :visible.sync="innerVisible" append-to-body top="150vh">
        <el-form ref="changeForm" :rules="changeForm" :model="changeForm" label-width="80px">
            <el-form-item label="转交人" >
              <el-select v-model="changeForm.approverUid" placeholder="转交人"
                         clearable
                         filterable>
                <el-option
                  v-for="item in userList"
                  :label="item.nickName"
                  :value="item.userId"
                  :key="item.userId"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="原 因" >
              <el-input
                type="textarea"
                :rows="2"
                placeholder="请输入内容"
                v-model="changeForm.changeRemark"
              >
              </el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitChangeForm">确 定</el-button>
          <el-button @click="innerVisible = false">取 消</el-button>
        </div>
      </el-dialog>
      <div slot="footer" class="dialog-footer">
        <el-button type="success" @click="submitForm('1')">同 意</el-button>
        <el-button type="primary" @click="changeButton">转 交</el-button>
        <el-button type="danger" @click="submitForm('-1')">拒 绝</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWorkflow, getWorkflow,updateWorkFlowApply, delWorkflow, addWorkflow, updateWorkflow, exportWorkflow } from "@/api/approval/workflow";
import { listApply, getApply, delApply, addApply, updateApply, exportApply,startApply,getCourse } from "@/api/approval/apply";
import {listBasicinfo} from "@/api/approval/basicinfo"
import {listUser} from "@/api/system/user"

export default {
  name: "Workflow",
  components: {
  },
  data() {
    return {
      //审核意见
      approverRemark:"",
      //用户
      userList:[],
      //流程进程
      flowList:[],
      //进程样式
      processStyle:"",
      //当前审核节点
      nodeId:null,
      //采购类型
      flowCaiGouApply:{
        arrCaigou:[]
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工作流程表格数据
      workflowList: [],
      //流程类型
      basicinfoList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示内
      innerVisible : false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        approverUid: null,
        status: null,
        upNodeId: null,
        downNodeId: null,
        approverRemark: null,
        orderNum: null,
        nodeType: null,
        approvalTime: null,
        changeTime: null,
        changeRemark: null,
        applyId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //转交表单
      changeForm:{},
      //转交表单验证
      changeRules:{},

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工作流程列表 */
    getList() {
      this.loading = true;
      listWorkflow(this.queryParams).then(response => {
        this.workflowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listBasicinfo().then(response =>{
        this.basicinfoList = response.rows
      });
      listUser().then(response =>{
        this.userList = response.rows
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        approverUid: null,
        status: "0",
        orderNum: null,
        nodeType: null,
        approvalTime: null,
        changeTime: null,
        changeRemark: null,
        applyId: null
      };
      this.approverRemark=""
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 详情及审核按钮操作 */
    handleUpdate(row) {
      this.changeForm = row
      this.reset();
      const id = row.applyId
      getApply(id).then(response => {
        this.form = response.data;
        this.flowCaiGouApply= this.form.flowCaiGouApply
        this.open = true;
        this.title = "详情及审核";
      });
      getCourse(row.applyId).then(res => {
        this.openCourse = true;
        this.flowList = res.rows
        let then = this
        for (let i = 0; i<then.flowList.length;i++) {
          if (then.flowList[i].status === "1") {
            this.processStyle = "finish"
            if (then.flowList.length-i!==1) {
              /*节点不为最后*/
              this.nodeId = i+1
            } else if (then.flowList[i].status === "1"&&then.flowList.length-i===1) {
              /*最后一个节点*/
              this.nodeId = then.flowList.length
            }
          }else if (then.flowList[i].status === "-1"){
            this.processStyle = "error"
            this.nodeId = i+1
          }
        }
      })
    },
    /** 转交按钮*/
    changeButton(){
      this.changeForm.approverUid = null
      this.innerVisible = true;
    },
    /** 转交提交按钮 */
    submitChangeForm(){
      addWorkflow(this.changeForm).then(response =>{
        this.msgSuccess("转交成功");
        this.open = false;
        this.innerVisible = false;
        this.changeForm = {}
        this.getList();
      })
    },
    /** 提交按钮 */
    submitForm(row) {
      this.form.status = row
      this.form.approverRemark = this.approverRemark
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateWorkFlowApply(this.form).then(response => {
              this.msgSuccess("审核成功");
              this.open = false;
              this.getList();
            });
          }
        });
      },
    }
};
</script>
<style scoped="scoped">
  .course-style {
    color: #1e1e1e;
  }
</style>
